﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PreguntaAlExperto.RulesLayer.Entities;
using PreguntaAlExperto.RulesLayer.Services;
using System.Collections.Generic;
using Crawler;
using Microsoft.Security.Application;
using Common;

namespace PreguntaAlExperto
{

    public partial class main : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptInclude(this.GetType(),"AjaxScript", "/script/expScript.js");

            this._header.Title = Resources.language.PageTitle;
            if (Request.FilePath.Contains("/articles/"))
            {
                try
                {
                    string _articleName = Request.FilePath.Replace("/articles/", "").Remove(Request.FilePath.Replace("/articles/", "").Length - 5, 5) + ".aspx";
                    ArticlesService _service = new ArticlesService();
                    Article _article = _service.getArticleByPath(_articleName);
                    this._header.Title += " - " + AntiXss.HtmlEncode(_article.Title);

                    HtmlMeta metaDescription = new HtmlMeta();
                    metaDescription.Name = "description";
                    metaDescription.Content = AntiXss.HtmlEncode(_article.Short_Description);

                    HtmlMeta metaKeywords = new HtmlMeta();
                    metaKeywords.Name = "keywords";
                    List<string> keywords = new List<string>(_article.AllCategories);
                    keywords.AddRange(_article.PublisherEntity.Name.Split(' '));
                    keywords.AddRange(_article.Title.Split(' '));
                    metaKeywords.Content = String.Join(", ", keywords.ToArray() ); 

                    this.Metas.Controls.Add(metaDescription);
                    this.Metas.Controls.Add(metaKeywords);
                }
                catch
                { }
            }

            string rootUrl = String.Format("{0}://{1}:{2}/",
                Request.Url.Scheme, Request.Url.Host,
                Request.Url.Port);

            HtmlLink linkEditURI = new HtmlLink();
            linkEditURI.Attributes.Add("type", "application/rsd+xml");
            linkEditURI.Attributes.Add("rel", "EditURI");
            linkEditURI.Attributes.Add("title", "RSD");
            linkEditURI.Attributes.Add("href", rootUrl + "rsd.ashx");

            HtmlLink linkWLWManifest = new HtmlLink();
            linkWLWManifest.Attributes.Add("type", "application/wlwmanifest+xml");
            linkWLWManifest.Attributes.Add("rel", "wlwmanifest");
            linkWLWManifest.Attributes.Add("href", rootUrl + "wlwmanifest.ashx");
            this.Links.Controls.Add(linkEditURI);
            this.Links.Controls.Add(linkWLWManifest);

            this.lnkContactUs.Text = Resources.language.ContactUs;
            this.lnkADPublish.Text = Resources.language.ADPublish;
            this.lnkDonate.Text = Resources.language.Donate;
            this.lnkPublishArt.Text = Resources.language.PublishArticle;
            this.lnkPublishLogged.Text = Resources.language.PublishArticle;
            this.lnkPublishUnlogged.Text = Resources.language.PublishArticle;
            this.sa.Text = Resources.language.Search;
            this.lnkLogin.Text = Resources.language.LoginLink;
            this.lnkRegister.Text = Resources.language.RegisterLink;
            this.lnkCloseSession.Text = Resources.language.CloseSession;
            this.lnkPrivacyPolicy.Text = Resources.language.PrivacyPolicy;

            this.lnkContactUs.NavigateUrl = "mailto:info@preguntaalexperto.net";
            this.lnkADPublish.NavigateUrl = "~/articles/ADPublish.aspx";
            this.lnkDonate.NavigateUrl = "~/articles/Donate.aspx";
            this.lnkPublishArt.NavigateUrl = "~/publicar.aspx";
            this.lnkPublishLogged.NavigateUrl = "~/publicar.aspx";
            this.lnkPublishUnlogged.NavigateUrl = "~/publicar.aspx";
            this.lnkRegister.NavigateUrl = "~/registrarse.aspx";
            this.lnkLogin.NavigateUrl = "~/ingresar.aspx";
            this.lnkPrivacyPolicy.NavigateUrl = "~/documents/PoliticaPrivacidad.aspx";

#if !SEARCHENABLED
            this.sa.PostBackUrl = "~/busqueda.aspx"; 
#endif

            if (Session["UserID"] != null)
            {
                this.UserDiv.Visible = false;
                this.lblUserName.Text = Session["UserName"].ToString();
            }
            else
            {
                this.RegisteredDiv.Visible = false;
            }
            
        }

        protected void lnkCloseSession_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Session.Clear();
            Response.Redirect("~/default.aspx");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
#if SEARCHENABLED
            //if (HttpContext.Current != null)
            //    HttpContext.Current.Response.Redirect("~/busqueda.aspx?q=" + HttpUtility.UrlEncode(this.txtQuery.Text));
#endif
        }
    }
}
